package cn.lyd.org.controller;

import cn.lyd.base.annotation.PreAuthorize;
import cn.lyd.base.until.AjaxResult;
import cn.lyd.base.until.LoginContext;
import cn.lyd.base.until.PageList;
import cn.lyd.org.domain.Department;
import cn.lyd.org.query.DepartmentQuery;
import cn.lyd.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(value = "部门的API",description="部门相关的CRUD功能")
@RestController //@controller +@ResponseBody 只返回json数据，不经过视图解析器，否则不能用
@RequestMapping("/dept")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /*restful接口*/

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询全部" )
    @PreAuthorize(name = "查询所有部门",sn = "department:getAll")
    public List<Department> findAll(){
        return departmentService.findAll();
    }

    /**
     * 查询一个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    @PreAuthorize(name = "查询单个部门",sn = "department:getOne")
    public Department findById(@PathVariable("id") Long id){
        return departmentService.findById(id);
    }

    /**
     * 添加或修改
     * @param department
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改" )
    @PreAuthorize(name = "部门保存",sn = "department:save")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            //判断有无id--判断是添加还是修改
            if (department.getId()==null){
                departmentService.add(department);
            }else {
                departmentService.update(department);
            }
            //操作成功调用AjaxResult的无参构造
            return new AjaxResult();
        } catch (Exception e) {//操作失败
            e.printStackTrace();
            //操作失败调用AjaxResult的有参构造
            return new AjaxResult(false,"操作失败");
        }

    }


    /**
     * 分页查询
     * @param departmentQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询" )
    @PreAuthorize( name = "部门列表",sn = "department:list")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery, HttpServletRequest request){

        //测试获取当前登录人的信息
        //Object userInfo = LoginContext.getLoginUser(request);
        //System.out.println(userInfo);

        return   departmentService.queryByPage(departmentQuery);
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除" )
    @PreAuthorize(name = "部门删除",sn = "department:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            //如果删除成功，调用AjaxResult的无参构造
            departmentService.delete(id);
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }


    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除对象" )
    @PreAuthorize(name = "部门批量删除",sn = "department:patchDel")
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
            //如果删除成功，调用AjaxResult的无参构造
            departmentService.batchDelete(ids);
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }


    /**
     * 查询部门树
     * @return
     */
    @GetMapping("/deptTree")
    @ApiOperation(value = "查询部门树" )
    @PreAuthorize(name = "查询部门树",sn = "department:deptTree")
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }


}
